// 部门列表处理
function format(data1) {
  let data = Object.keys(data1).filter((ele) => isNaN(ele)).length
    ? [data1]
    : Object.values(data1);
  let temp = [];
  for (let i = 0; i < data.length; i++) {
    if (
      !data[i].subordinateEmpDepts ||
      !Object.keys(data[i].subordinateEmpDepts).length
    ) {
      if (!data[i].erms || !Object.keys(data[i].erms).length) {
        temp.push({
          ...data[i]
        });
      } else {
        let arr = [];
        for (let key in data[i].erms) {
          arr.push(data[i].erms[key]);
        }
        data[i].erms = arr;
        temp.push({
          ...data[i],
          erms: format(data[i].erms)
        });
      }
    } else {
      let arr = [];
      for (let key in data[i].subordinateEmpDepts) {
        arr.push(data[i].subordinateEmpDepts[key]);
      }
      data[i].subordinateEmpDepts = arr;
      temp.push({
        ...data[i],
        subordinateEmpDepts: format(data[i].subordinateEmpDepts)
      });
    }
  }
  return temp;
}

function changeTree(data) {
  let treeData = JSON.stringify(format(data))
    .replace(/erms/g, 'children')
    .replace(/empId/g, 'deptId')
    .replace(/empName/g, 'name')
    .replace(/subordinateEmpDepts/g, 'children')
    .replace(/deptName/g, 'name');
  return JSON.parse(treeData);
}

function getIdAndName(arr, value, label, children, relId) {
  if (!relId) return;
  let name;
  for (let i = 0; i < arr.length; i++) {
    let item = arr[i];
    if (item[value] == relId) {
      name = item[label];
      break;
    } else {
      if (item[children]) {
        getIdAndName(item[children], value, label, children, relId);
      }
    }
  }
  console.log(name);
  return name;
}

function formatToArr(data, keys, child) {
  let trueData = Object.keys(data).filter((ele) => isNaN(ele)).length
    ? [data]
    : Object.values(data);
  let copyData = JSON.parse(JSON.stringify(trueData));
  let temp = [];
  for (let i = 0; i < copyData.length; i++) {
    let dataObj = {
      [child]: []
    };
    keys.forEach((key) => {
      if (copyData[i]) {
        if (!copyData[i][key] || !Object.keys(copyData[i][key]).length) {
          dataObj = {
            ...dataObj,
            ...copyData[i],
            name:
              copyData[i]['deptName'] ||
              copyData[i]['empName'] ||
              copyData[i]['name']
          };
        } else {
          copyData[i][key] = Object.values(copyData[i][key]);
          let value = formatToArr(copyData[i][key], keys, child);
          let children = JSON.parse(
            JSON.stringify([...dataObj[child], ...value])
          );
          let personList = [];
          [...dataObj[child], ...value].forEach((ele) => {
            if (ele.empName && !ele.deptName) {
              personList.push(ele);
              let idx = children.findIndex((e) => e.name === ele.name);
              children.splice(idx, 1);
            }
          });
          children.unshift(...personList);
          dataObj = {
            ...dataObj,
            ...copyData[i],
            name:
              copyData[i]['deptName'] ||
              copyData[i]['empName'] ||
              copyData[i]['name'],
            [key]: value,
            [child]: children
          };
        }
      }
    });
    temp.push(dataObj);
  }
  return temp;
}

function getType(obj) {
  return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}

export default {
  format,
  changeTree,
  getIdAndName,
  formatToArr,
  getType
};
